package com.atguigu.gmall.order.service.impl;

import com.atguigu.gmall.model.order.OrderDetail;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.order.mapper.OrderDetailMapper;
import com.atguigu.gmall.order.service.OrderDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author lfy
* @description 针对表【order_detail_1(订单明细表)】的数据库操作Service实现
* @createDate 2022-03-21 16:49:50
*/
@Service
public class OrderDetailServiceImpl extends ServiceImpl<OrderDetailMapper, OrderDetail>
    implements OrderDetailService{

    @Transactional
    @Override
    public void saveOrderDetail(OrderInfo preparedOrder) {
        List<OrderDetail> details = preparedOrder.getOrderDetailList().stream()
                .map(item -> {
                    OrderDetail detail = new OrderDetail();

                    detail.setOrderId(preparedOrder.getId()); //关联的订单id
                    detail.setSkuId(item.getSkuId());
                    detail.setUserId(preparedOrder.getUserId());
                    detail.setSkuName(item.getSkuName());
                    detail.setImgUrl(item.getImgUrl());
                    detail.setOrderPrice(item.getOrderPrice());
                    detail.setSkuNum(item.getSkuNum());
                    detail.setCreateTime(new Date());
                    detail.setSplitTotalAmount(item.getOrderPrice()
                            .multiply(new BigDecimal(item.getSkuNum() + "")));
                    detail.setSplitActivityAmount(new BigDecimal("0"));
                    detail.setSplitCouponAmount(new BigDecimal("0"));

                    return detail;
                })
                .collect(Collectors.toList());

        saveBatch(details);
    }
}




